package com.example.bishetianqi;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;

import com.example.bishetianqi.db.DBManager;

import java.util.ArrayList;
import java.util.List;

public class DeleteCityActivity extends AppCompatActivity implements View.OnClickListener {

    ImageView erriv,rightiv;
    ListView deleteLv;
    List<String>mDatas;//ListView的信息源
    List<String>deletecitys;//删除的城市集合
    private DeleteCityAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delete_city);
        erriv=findViewById(R.id.delete_iv_error);
        rightiv=findViewById(R.id.delete_iv_right);
        deleteLv=findViewById(R.id.delete_lv);
        //数据库操作
        mDatas=DBManager.queryAllCity();
        deletecitys=new ArrayList<>();
        //设置点击事件
        erriv.setOnClickListener(this);
        rightiv.setOnClickListener(this);
        //设置适配器
        adapter = new DeleteCityAdapter(this, mDatas, deletecitys);
        deleteLv.setAdapter(adapter);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.delete_iv_error:
                AlertDialog.Builder builder=new AlertDialog.Builder(this);
                builder.setTitle("提示信息").setMessage("您确定舍弃更改？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                finish();
                            }
                        });
                builder.setNegativeButton("取消",null);
                builder.create().show();
                break;
            case R.id.delete_iv_right:
                for (int i=0;i<deletecitys.size();i++){
                    String city=deletecitys.get(i);
                    //数据库操作
                    DBManager.deleteInfoByCity(city);
                }
                finish();
                break;
        }
    }
}
